package evanyark.weibo.view;

import java.util.List;

import weibo4android.Weibo;
import weibo4android.WeiboException;
import weibo4android.http.RequestToken;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import evanyark.weibo.R;
import evanyark.weibo.model.UserInfo;
import evanyark.weibo.util.Constants;
import evanyark.weibo.util.OAuthConstant;

public class LoginActivity extends ListActivity implements OnItemClickListener,
		OnItemLongClickListener {
	private ListView listView;
	private List<UserInfo> userList;
	private UserInfo currentUser;
	private ImageView user_default_headicon;
	private LinearLayout account_add_btn_bar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.account);

		setUpViews();// 设置view
		setUpListeners();// 设置listenter

		System.setProperty("weibo4j.oauth.consumerKey", Constants.CONSUMER_KEY);
		System.setProperty("weibo4j.oauth.consumerSecret",
				Constants.CONSUMER_SECRET);
	}

	private void setUpViews() {
		listView = getListView();
		user_default_headicon = (ImageView) findViewById(R.id.user_default_headicon);
		account_add_btn_bar = (LinearLayout) findViewById(R.id.account_add_btn_bar);
	}

	private void setUpListeners() {
		user_default_headicon.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				SharedPreferences preferences = getSharedPreferences(
						"default_user", Activity.MODE_PRIVATE);
				SharedPreferences.Editor editor = preferences.edit();
				editor.putString("user_default_nick", currentUser.getUserName());
				editor.putString("user_default_name", currentUser.getUserId());
				editor.commit();
				Intent intent = new Intent(LoginActivity.this,
						MainActivity.class);
				startActivity(intent);
			}
		});
		account_add_btn_bar.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				Weibo weibo = OAuthConstant.getInstance().getWeibo();
				RequestToken requestToken;
				try {
					requestToken = weibo
							.getOAuthRequestToken("weibotogether://OAuthActivity");
					Uri uri = Uri.parse(requestToken.getAuthenticationURL()
					/* + "&from=xweibo" */);
					OAuthConstant.getInstance().setRequestToken(requestToken);
					startActivity(new Intent(Intent.ACTION_VIEW, uri));
				} catch (WeiboException e) {
					e.printStackTrace();
				}
			}
		});
		listView.setOnItemLongClickListener(this);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO:
		((MainActivity) getParent()).setCurrentTab(2);

		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {

		return false;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		currentUser = userList.get(position);
		user_default_headicon.setImageDrawable(currentUser.getUserIcon());
	}

}
